package org.edu.modules.base.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.modules.base.entity.PageBean;
import org.edu.modules.base.entity.VeBaseAppManage;
import org.edu.modules.base.service.IVeBaseAppManageService;
import org.edu.modules.base.service.IVeBaseSysMenuService;
import org.edu.modules.base.service.IVeBaseSysRoleMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Slf4j
@Api(tags = {"应用管理"})
@RestController
@RequestMapping("/base/VeBaseAppManage")
@ApiSort(2)
public class VeBaseAppManageController {

    @Autowired
    private IVeBaseAppManageService veBaseAppManageService;

    @Autowired
    private IVeBaseSysMenuService veBaseSysMenuService;
    @Autowired
    private IVeBaseSysRoleMenuService veBaseSysRoleMenuService;

    @AutoLog("应用信息-获取所有")
    @ApiOperation(value = "应用信息-获取所有", notes = "应用信息-获取所有")
    @GetMapping({"/getAppManageList"})
    public Result<?> getAppManageList() {
        List<VeBaseAppManage> list = this.veBaseAppManageService.getAppManageAllList(null, "0");
        return Result.ok(list);
    }

    @AutoLog("应用信息-分页列表查询")
    @ApiOperation(value = "应用信息-分页列表查询", notes = "应用信息-分页列表查询")
    @GetMapping({"/appManagePageList"})
    public Result<?> appManagePageList(VeBaseAppManage appManage, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        int num = this.veBaseAppManageService.getAppManageAllListNum(appManage.getAppName(), appManage.getIsEnable());
        PageBean pb = new PageBean(pageNo.intValue(), pageSize.intValue(), num);
        pb.setList(this.veBaseAppManageService.getAppManagePageList(appManage.getAppName(), appManage.getIsEnable(), Integer.valueOf(pb.getStartIndex()), pageSize));
        return Result.ok(pb);
    }

    @AutoLog("应用信息-通过id查询")
    @ApiOperation(value = "应用信息-通过id查询", notes = "应用信息-通过id查询")
    @GetMapping({"/appManageById"})
    public Result<?> appManageById(@RequestParam(name = "id", required = true) String id) {
        VeBaseAppManage appManage = (VeBaseAppManage) this.veBaseAppManageService.getById(id);
        if (appManage == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appManage);
    }

    @AutoLog("应用信息-添加")
    @ApiOperation(value = "应用信息-添加", notes = "应用信息-添加")
    @PostMapping({"/appManageAdd"})
    public Result<?> appManageAdd(@RequestBody VeBaseAppManage appManage) {
        VeBaseAppManage model = this.veBaseAppManageService.getAppManageByName(null, appManage.getAppName());
        if (model != null) {
            return Result.error("应用名称已存在!");
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        appManage.setId(uuid);
        this.veBaseAppManageService.save(appManage);
        return Result.ok("添加成功!");
    }

    @AutoLog("应用信息-修改")
    @ApiOperation(value = "应用信息-修改", notes = "应用信息-修改")
    @PostMapping({"/appManageEdit"})
    public Result<?> appManageEdit(@RequestBody VeBaseAppManage appManage) {
        this.veBaseAppManageService.updateById(appManage);
        return Result.ok("修改成功!");
    }

    @AutoLog("应用信息-禁用")
    @ApiOperation(value = "应用信息-禁用", notes = "应用信息-禁用")
    @PostMapping({"/appManageStop"})
    public Result<?> appManageStop(@RequestParam(name = "id", required = true) String id) {
        VeBaseAppManage appManage = (VeBaseAppManage) this.veBaseAppManageService.getById(id);
        if (appManage != null) {
            appManage.setIsEnable("1");
            this.veBaseAppManageService.updateById(appManage);
        }
        return Result.ok("禁用成功!");
    }

    @AutoLog("应用信息-启用")
    @ApiOperation(value = "应用信息-启用", notes = "应用信息-启用")
    @PostMapping({"/appManageStart"})
    public Result<?> appManageStart(@RequestParam(name = "id", required = true) String id) {
        VeBaseAppManage appManage = (VeBaseAppManage) this.veBaseAppManageService.getById(id);
        if (appManage != null) {
            appManage.setIsEnable("0");
            this.veBaseAppManageService.updateById(appManage);
        }
        return Result.ok("启用成功!");
    }

    @AutoLog("应用信息-通过id删除")
    @ApiOperation(value = "应用信息-通过id删除", notes = "应用信息-通过id删除")
    @PostMapping({"/appManageDelete"})
    @Transactional
    public Result<?> appManageDelete(@RequestParam(name = "id", required = true) String id) {
        this.veBaseAppManageService.removeById(id);
        this.veBaseSysMenuService.deleteByAppId(id);
        this.veBaseSysRoleMenuService.deleteModelByAppId(null, id);
        return Result.ok("删除成功!");
    }

    @AutoLog("应用信息-批量删除")
    @ApiOperation(value = "应用信息-批量删除", notes = "应用信息-批量删除")
    @PostMapping({"/appManageDeleteBatch"})
    @Transactional
    public Result<?> appManageDeleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.veBaseAppManageService.removeByIds(list);
        if (list.size() > 0) {
            for (String id : list) {
                this.veBaseSysMenuService.deleteByAppId(id);
                this.veBaseSysRoleMenuService.deleteModelByAppId(null, id);
            }
        }
        return Result.ok("批量删除成功!");
    }
}
